package wiki;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.Root;

/**
 * Servlet implementation class GetFeedback
 */
public class GetFeedback extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetFeedback() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();

		Root root = new Root();
		ResultSet rs=null;
		ResultSet rscount = null;
		String sql, sql1 = "";
		JSONObject feedback;
		JSONArray feedbacklist;

		PreparedStatement pst =null;
		feedbacklist = new JSONArray();

		JSONObject feedbacks = new JSONObject();
		
		try {

			root.initConnection();

			sql = "select * from feedback where deleteflag = 0 order by id desc";

			pst = root.con.prepareStatement(sql);
			// System.out.println(pst);

			rs = pst.executeQuery();

			while (rs.next()) {
				feedback = new JSONObject();
				feedback.put("id", rs.getString("id"));
				feedback.put("feedback", rs.getString("feedback"));

				feedback.put("user", rs.getString("username"));
				feedback.put("date", rs.getString("date"));
				
				feedbacklist.add(feedback);
			}
			
			
			feedbacks.put("feedback", feedbacklist);

		} catch (Exception e) {
			System.out.println("Exception getting issue: " + e.toString());
		}finally{
			try {
				pst.close();
				rs.close();
				root.closeConnection();
			} catch (Exception e) {
				
			}
			
		}

		out.println(feedbacks);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

}
